package moviescraper.doctord.view;

import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import moviescraper.doctord.model.preferences.MoviescraperPreferences;
import moviescraper.doctord.scraper.DitzyHeadlessBrowser;
import moviescraper.doctord.scraper.DitzyHeadlessBrowserSingle;

public class SettingsDialogJAVLibrary extends javax.swing.JDialog {

	private final MoviescraperPreferences preferences;
	private final String savedUserAgent;
	private final String savedCookieJar;

	/**
	 * Creates new form SettingsDialogJAVLibrary
	 * 
	 * @param parent Parent frame
	 */
	public SettingsDialogJAVLibrary(java.awt.Frame parent) {
		super(parent, true);
		initComponents();

		//this.setTitle("JAVLibrary settings");
		setLocationRelativeTo(parent);
		preferences = MoviescraperPreferences.getInstance();
		savedUserAgent = preferences.getUserAgent();
		savedCookieJar = preferences.getCookieJar();
		userAgentInput.setText(savedUserAgent);
		cookieJarInput.setText(savedCookieJar);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		userAgentInput = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		cookieJarInput = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("JAV L");
		setLocationByPlatform(true);
		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
		setPreferredSize(new java.awt.Dimension(500, 150));

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Para"));
		jPanel1.setLayout(new java.awt.GridLayout(0, 2));

		jLabel1.setText("User Agent");
		jPanel1.add(jLabel1);

		userAgentInput.setToolTipText("The user agent used to generate to cookie jar.\n\nExemple: Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0");
		jPanel1.add(userAgentInput);

		jLabel2.setText("Cookie jar file");
		jPanel1.add(jLabel2);

		cookieJarInput.setToolTipText("The path to the cookie jar file.\n\nCookie jar is curl formatted. One can use cookies.txt plugin for firefox to generate this file");
		cookieJarInput.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				cookieJarInputMouseClicked(evt);
			}
		});
		jPanel1.add(cookieJarInput);

		getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

		jButton1.setText("OK");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});
		getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		DitzyHeadlessBrowser browser = DitzyHeadlessBrowserSingle.getBrowser();
		try {
			preferences.setUserAgent(userAgentInput.getText());
			preferences.setCookieJar(cookieJarInput.getText());
			browser.configure();
			MoviescraperPreferences.savePreferences();
			this.setVisible(false);
		} catch (IOException ex) {
			preferences.setUserAgent(savedUserAgent);
			preferences.setCookieJar(savedCookieJar);
			JOptionPane.showMessageDialog(this, "File " + cookieJarInput.getText() + " does not exists. Please pick an existing file", "Error", JOptionPane.ERROR_MESSAGE);
		}

	}//GEN-LAST:event_jButton1ActionPerformed

	private void cookieJarInputMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cookieJarInputMouseClicked
		JFileChooser chooser = new JFileChooser();
		int returnVal = chooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			cookieJarInput.setText(chooser.getSelectedFile().getPath());
		}
	}//GEN-LAST:event_cookieJarInputMouseClicked

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(SettingsDialogJAVLibrary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(SettingsDialogJAVLibrary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(SettingsDialogJAVLibrary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(SettingsDialogJAVLibrary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the dialog */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				SettingsDialogJAVLibrary dialog = new SettingsDialogJAVLibrary(new javax.swing.JFrame());
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JTextField cookieJarInput;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JTextField userAgentInput;
	// End of variables declaration//GEN-END:variables
}
